import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:kq_flutter_core_widget/widgets/table/table.dart';
import 'package:kq_flutter_core_widget/widgets/table/render/kq_text_cell_render.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../resources/images.dart';

/// 表格组件Demo
class TableDemoPage extends StatefulWidget {
  const TableDemoPage({super.key});

  @override
  State<StatefulWidget> createState() => _TableDemoPageState();
}

class _TableDemoPageState extends State<TableDemoPage> {
  final TableController _tableController = TableController();
  int updateTime = DateTime.now().microsecondsSinceEpoch;
  final GlobalKey _tableTopRow = GlobalKey();
  final GlobalKey _tableTopRow2 = GlobalKey();

  var tableData = [
    [
      TableData(text: "序号"),
      TableData(text: "计划单号"),
      TableData(text: "任务单号"),
      TableData(text: "产品名称"),
      TableData(text: "订单编号"),
      TableData(text: "产品编号"),
      TableData(text: "模具编号"),
      TableData(text: "模具名称"),
      TableData(text: "产品重量"),
      TableData(text: "是否可用"),
      TableData(text: "操作"),
    ],
    for (int i = 0; i < 99; i++)
      [
        TableData(text: "${i + 1}"),
        TableData(text: "P100000$i"),
        TableData(text: "T20000$i"),
        TableData(text: "产品名称$i"),
        TableData(text: "NO.2359xxxxxxxxxxx$i"),
        TableData(text: "PNO.5679$i"),
        TableData(text: "MNO.3568$i"),
        TableData(text: "模具名称$i"),
        TableData(text: "${Random().nextInt(1000)}g"),
        TableData(valid: Random().nextInt(1000) > 50),
        TableData(text: "删除", delete: true),
      ]
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('表格工具演示'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          color: Colors.white,
          onPressed: Get.back,
        ),
      ),
      body: Column(
        children: [
          Visibility(
              visible: false,
              child: InkWell(
                key: _tableTopRow2,
                onTap: () => setState(() {
                  if (tableData.length > 50) {
                    tableData.removeRange(40, 100);
                    setState(() {});
                  } else {
                    for (int i = tableData.length - 1; i < 100; i++) {
                      tableData.add([
                        TableData(text: "${i + 1}"),
                        TableData(text: "P100000$i"),
                        TableData(text: "T20000$i"),
                        TableData(text: "产品名称$i"),
                        TableData(text: "NO.2359$i"),
                        TableData(text: "PNO.5679$i"),
                        TableData(text: "MNO.3568$i"),
                        TableData(text: "模具名称$i"),
                        TableData(text: "${Random().nextInt(1000)}g"),
                        TableData(valid: Random().nextInt(1000) > 50),
                        TableData(text: "删除", delete: true),
                      ]);
                    }
                    setState(() {});
                  }
                }),
                child: ColoredBox(
                  key: _tableTopRow,
                  color: Colors.white,
                  child: Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    child: const Padding(
                      padding:
                          EdgeInsets.symmetric(vertical: 20, horizontal: 30),
                      child: Text("改变数据"),
                    ),
                  ),
                ),
              )),
          Expanded(
            child: EasyRefresh.builder(
              header: const ClassicHeader(),
              onRefresh: () async {
                await Future.delayed(const Duration(seconds: 2));
                return Future(() => null);
              },
              childBuilder: (BuildContext context, ScrollPhysics physics) {
                return _buildTable(physics);
              },
            ),
          )
        ],
      ),
    );
  }

  Widget _buildTable(ScrollPhysics scrollPhysics) {
    return KqTable(
      columnCount: tableData[0].length,
      rowCount: tableData.length,
      updateTime: updateTime,
      fixTopCount: 1,
      fixLeftCount: 1,
      syncInitAllColumnWidth: true,
      syncInitAllRowHeight: true,
      tableController: _tableController,
      verticalScrollPhysics: scrollPhysics,
      cellBoxConstraints: (row, column) => const BoxConstraints(
          minWidth: 50, maxWidth: 200, minHeight: 50, maxHeight: 200),
      couldDragChangeWidth: (row, column) => row == 0,
      couldDragChangeHeight: (row, column) => column == 0,
      cellWidthMeasure: (int row, int column) => 100,
      cellHeightMeasure: (int row, int column) => 50,
      cellRender: _buildCellRender(),
      headers: const [
        SliverToBoxAdapter(
          child: ColoredBox(
            color: Colors.white,
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
              child: Text(
                "表格头",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
              ),
            ),
          ),
        ),
      ],
      footers: const [
        SliverToBoxAdapter(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 50),
            child: Text("表格底部内容1"),
          ),
        ),
      ],
    );
  }

  CellRender _buildCellRender() {
    var cellConfig = KqTextCellConfig(
        borderColor: Colors.grey,
        alignment: Alignment.center,
        borderWidth: 1,
        longPressBackground: Colors.amberAccent,
        isMultiLine: true,
        maxLines: 2,
        textOverflow: TextOverflow.ellipsis);

    return CustomCellRender(
      (row, column) => tableData[row][column],
      cellConfigBuilder: (row, column) {
        cellConfig.background =
            row == 0 || column == 0 ? Colors.grey[300] : null;
        var data = tableData[row][column];
        if (data.delete == true) {
          cellConfig.textStyle = const TextStyle(color: Colors.blue);
        } else {
          cellConfig.textStyle = const TextStyle(color: Colors.black);
        }
        return cellConfig;
      },
      highLightBuilder: _buildHighLight,
      onTapBuilder: (row, column) {
        if (tableData[row][column].delete == true) {
          return (row, column) {
            setState(() {
              tableData.removeAt(row);
            });
          };
        }

        return null;
      },
    );
  }

  HighLight? _buildHighLight(int row, int column) {
    if (row == 0 && column == 0) {
      return HighLight(Colors.blue[50]!, row: 0, column: 0);
    } else if (row == 0) {
      return HighLight(Colors.blue[50]!, column: column);
    } else if (column == 0) {
      return HighLight(Colors.blue[50]!, row: row);
    } else {
      return null;
    }
  }
}

class CustomCellRender extends KqTextCellRender<TableData> {
  CustomCellRender(super.dataBuilder,
      {super.cellConfigBuilder, super.onTapBuilder, super.highLightBuilder});

  @override
  Widget buildBody(BuildContext context, int row, int column, double width,
      double height, bool isLongPress) {
    var data = dataBuilder.call(row, column);
    if (data.valid != null) {
      Image image;
      var size = 30.0;
      if (data.valid == true) {
        image = Image.asset(Images.right, width: size, height: size);
      } else {
        image = Image.asset(Images.error, width: size, height: size);
      }
      return Align(alignment: Alignment.center,child: image);
    }

    return super.buildBody(context, row, column, width, height, isLongPress);
  }

  @override
  String getCellText(TableData t) {
    return t.text ?? "";
  }
}

class TableData {
  String? text;
  Color? textColor;
  bool? valid;
  bool? delete;

  TableData({this.text, this.delete, this.textColor, this.valid});
}
